package com.kkb.service.impl;

import com.kkb.domain.AjaxResult;
import com.kkb.mapper.TbGoodsMapper;
import com.kkb.mapper.TbImageMapper;
import com.kkb.pojo.*;
import com.kkb.service.TbGoodsService;
import com.kkb.vo.TbGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.awt.*;
import java.util.Date;
import java.util.List;


@Service
public class TbGoodsServiceImpl implements TbGoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    @Autowired
    private TbImageMapper tbImageMapper;



    @Override
    public List<TbGoodsVO> getGoodsVoList() {
        return tbGoodsMapper.getGoodsVoList();
    }

    @Override
    public TbGoods selectTbGoodsById(Integer goodsId) {
        TbGoods goods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        return goods;
    }

    @Override
    @Transactional
    public AjaxResult updateGoods(TbGoods tbGoods) {
        String url=tbGoods.getBak1();
        TbGoods goods = tbGoodsMapper.selectByPrimaryKey(tbGoods.getGoodsId());
        goods.setStatus(tbGoods.getStatus());
        goods.setIsDelete("N");
        goods.setCostPrice(tbGoods.getCostPrice());
        goods.setInventory(tbGoods.getInventory());
        goods.setGoodsName(tbGoods.getGoodsName());
        goods.setSellPrice(tbGoods.getSellPrice());
        goods.setUpdateTime(new Date());
        goods.setBak1(null);
        int result = tbGoodsMapper.updateByPrimaryKey(goods);
        //更新图片路径操作
        if(url!=null){
            TbImageExample example = new TbImageExample();
            TbImageExample.Criteria criteria = example.createCriteria();
            criteria.andGoodsIdEqualTo(tbGoods.getGoodsId());
            //如果原来就没有图片，则新增，有图片则修改
            List<TbImage> tbImages = tbImageMapper.selectByExample(example);
            if(tbImages!=null){
                TbImage image = tbImages.get(0);
                image.setImgPath(url);
                image.setUpdateTime(new Date());
                tbImageMapper.updateByPrimaryKey(image);
            }else{
                TbImage image = new TbImage();
                image.setGoodsId(goods.getGoodsId());
                image.setImgPath(url);
                image.setCreateTime(new Date());
                tbImageMapper.insert(image);
            }

        }
        if(result>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @Override
    @Transactional
    public AjaxResult removeGoodsById(Integer goodsId) {
        int goodsDelCount=tbGoodsMapper.deleteByPrimaryKey(goodsId);
        if(goodsDelCount<1){
            return AjaxResult.error("商品删除失败！");
        }
        //同时移除img表里的记录
        TbImageExample example = new TbImageExample();
        TbImageExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        //不用判断图片删除结果，因为可能没有图片
        tbImageMapper.deleteByExample(example);

        return AjaxResult.success();
    }

}
